package com.cn.client;

import java.util.Random;

public class ThreadLocalIndex {

    private final ThreadLocal<Integer> threadLocal = new ThreadLocal<>();
    private final Random random = new Random();

    public Integer getAndIncrement() {
        Integer index = this.threadLocal.get();
        if (index == null) {
            index = Math.abs(random.nextInt());
            if (index < 0) index = 0;
            this.threadLocal.set(index);
        }
        index = this.threadLocal.get();
        if (index < 0) index = 0;
        return index;
    }

    @Override
    public String toString() {
        return "ThreadLocalIndex{" +
                "threadLocal=" + threadLocal +
                ", random=" + random +
                '}';
    }
}
